package Vista;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import GestionDeDatos.GestorBD;
import InfoCliente.Inscripcion;
import InfoCliente.UsuarioActivo;

public class CargarHorarios {
	
	private CardLayout cardlayout = new CardLayout();
	private JPanel mainPanel = new JPanel(cardlayout);

	private static final String Depto = "Depto";
	private static final String Materia = "Materia";
	private static final String Curso = "Curso";
	private static final String Eliminar = "Eliminar";
	
	private JComboBox<String> jcb_depto, jcb_materia, jcb_curso, jcb_matel;
	private JButton jbt_sigMat, jbt_sigCurso, jbt_cargar, jb_eliminar, jb_subEliminar;
	private JLabel jlb_intro, jlb_intro2, jlb_intro3, jlb_intro4, jlb_depto, jlb_materia, jlb_curso;
	
	public CargarHorarios() {
		initComponents();
	}
	
	protected void initComponents() {
		
		mainPanel.add(initSolapaDepto(), Depto);
	    mainPanel.add(initSolapaMateria(), Materia);
	    mainPanel.add(initSolapaCurso(), Curso);
	    mainPanel.add(initSolapaEliminar(), Eliminar);
	    mainPanel.setSize(530, 450);
		
	    SigMateriaActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 
	        	 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        	 String[][] data = {};
	        	 try {
	        		 data = GestorBD.Gestor().ObtenerMaterias(usuarioActivo, getDepto()).getRegisters();
	        	 } catch (Exception exception) {
	        		 JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
	     			 return; 
	        	 }
	        	 
	        	 if (data.length > 0) {
						getJComboBoxMateria().removeAllItems();
						for (int i=0; i < data.length; i++ ) {
							getJComboBoxMateria().addItem(data[i][0] + " - " + data[i][1]);
						}
					} else {
						getJComboBoxMateria().removeAllItems();
						getJComboBoxMateria().addItem("(vacio)");
					}
	        	 
	        	 switchPanel(Materia);
	         }
	      });
	    
	    SigCursoActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 
	        	 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        	 String[][] data = {};
	        	 try {
	        		 data = GestorBD.Gestor().ObtenerCursos(usuarioActivo, getDepto(), getMateria()).getRegisters();
	        	 } catch (Exception exception) {
	        		 JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
	     			 return; 
	        	 }
	        	 
	        	 if (data.length > 0) {
						getJComboBoxCurso().removeAllItems();
						for (int i=0; i < data.length; i++ ) {
							getJComboBoxCurso().addItem(data[i][0] + " - " + data[i][1]);
						}
					} else {
						getJComboBoxCurso().removeAllItems();
						getJComboBoxCurso().addItem("(vacio)");
					}
	        	 
	        	 switchPanel(Curso);
	         }
	      });
	    
	    CargarActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 
	        	 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        	 
	        	 try {
	        		 Inscripcion inscripcion = new Inscripcion(View.id, getDepto(), getMateria(), getCurso());
	        		 GestorBD.Gestor().RealizarInscripcion(usuarioActivo, inscripcion);
	        		 
	        	 } catch (NumberFormatException exception) {
	        		 JOptionPane.showMessageDialog(mainPanel,"Materia invalida", "Mensaje", JOptionPane.PLAIN_MESSAGE);	
	        	 
	         	 } catch (Exception exception) {
	        		 JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
	     			 return;
	        	 }
	        	 
	        	 switchPanel(Depto);
	         }
	      });
	    
	    EliminarActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        	 String[] fields = {"codigoMateria", "materia", "curso", "catedra"};
	        	 String[][] items = {};
	        	 String[] elements = {};
	        	 Set<String> set = new HashSet<String>();
	        	 try {
	        		 items = GestorBD.Gestor().ObtenerInscripciones(usuarioActivo).getRegisters(fields);
	        		 for (int k=0; k<items.length; k++) {
		        		 set.add(items[k][0] + "-" + items[k][1] + "-" + items[k][2] + "-" + items[k][3]);
	        		 }
	        		 elements = set.toArray(new String[0]);
	        		 
	        	 } catch (Exception exception) {
	        		 
	        	 }
	        	 jcb_matel.removeAllItems();
	        	 for (int i=0; i<elements.length; i++) {
	        		 jcb_matel.addItem(elements[i]);
	        	 }
	        	 
	        	 switchPanel(Eliminar);
	         }
	      });

	    SubEliminarActionListener(new ActionListener() {
	         @Override
	         public void actionPerformed(ActionEvent e) {
	        	 
	        	 UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
	        	 String selec = (String) jcb_matel.getSelectedItem();
	        	 String[] parts = selec.split("-");
	        	 int depto = Integer.parseInt(parts[0]);
	        	 depto = depto / 100;    
	        	 int materia = Integer.parseInt(parts[0]);
	        	 Inscripcion inscripcion = new Inscripcion(View.id,depto,materia,parts[2]);
	        	 
	        	 try {
					GestorBD.Gestor().DeshacerInscripcion(usuarioActivo, inscripcion);
				} catch (Exception exception) {
					JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
				}	 
	        	 switchPanel(Depto);
	         }
	      });
	}
	
	private int getDepto() {
		int i = jcb_depto.getSelectedIndex();
		
		UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
		String[][] data = {};
   	 	try {
   	 		data = GestorBD.Gestor().ObtenerDepartamentos(usuarioActivo).getRegisters();
   	 	} catch (Exception exception) {
   	 		JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
   	 	}
				
		return Integer.parseInt(data[i][0]);
	}
	
	private int getMateria() {
		int i = jcb_materia.getSelectedIndex();
		
		UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
		String[][] data = {};
   	 	try {
   	 		data = GestorBD.Gestor().ObtenerMaterias(usuarioActivo, getDepto()).getRegisters();
   	 	} catch (Exception exception) {
   	 		JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
   	 	}
				
		return Integer.parseInt(data[i][0]);		
	}
	
	private String getCurso() {
		int i = jcb_curso.getSelectedIndex();
		
		UsuarioActivo usuarioActivo = new UsuarioActivo(View.id, View.pass);
		String[][] data = {};
   	 	try {
   	 		data = GestorBD.Gestor().ObtenerCursos(usuarioActivo, getDepto(), getMateria()).getRegisters();
   	 	} catch (Exception exception) {
   	 		JOptionPane.showMessageDialog(mainPanel,exception.getMessage(), "Mensaje", JOptionPane.PLAIN_MESSAGE);	
   	 	}
				
		return data[i][0];	
	}

	protected JPanel initSolapaDepto() {
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.add(getJLabelIntro());
		panel.add(getJLabelDepto());
		panel.add(getJButtonSiguienteMat());
		panel.add(getJComboBoxDepto());
		panel.add(getJButtonEliminar());
		panel.setSize(530, 450);
		return panel;
	}
	
	protected JPanel initSolapaMateria() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.add(getJLabelIntro2());
		panel.add(getJLabelMateria());
		panel.add(getJButtonSiguienteCurso());
		panel.add(getJComboBoxMateria());
		//panel.add(getJButtonEliminar2());
		panel.setSize(530, 450);
		return panel;
	}
	
	protected JPanel initSolapaCurso() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.add(getJLabelIntro3());
		panel.add(getJLabelCurso());
		panel.add(getJButtonCargar());
		panel.add(getJComboBoxCurso());
		//panel.add(getJButtonEliminar3());
		panel.setSize(530, 450);
		return panel;
	}
	
	protected JPanel initSolapaEliminar() {
		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.add(getJLabelIntro4());
		panel.add(getJComboBoxMaterias()); 
		panel.add(getJButtonEliminar4());
		panel.setSize(530, 450);
		return panel;
	}

	public void switchPanel(String key) {
		cardlayout.show(mainPanel, key);
	}
	
	private JLabel getJLabelIntro4() {
		if (jlb_intro4 == null) {
			jlb_intro4 = new JLabel("Eliminar Horarios");
			Font newTextFieldFont=new Font(jlb_intro4.getFont().getName(),jlb_intro4.getFont().getStyle(),18);   
		    jlb_intro4.setFont(newTextFieldFont);  
		    jlb_intro4.setBounds(50, 20, 400, 50);
		}

		return jlb_intro4;
	}
	
	private JButton getJButtonEliminar4() {
		if (jb_subEliminar == null) {
			jb_subEliminar = new JButton();
			jb_subEliminar.setText("Eliminar");
	        jb_subEliminar.setMargin(new Insets(1, 5, 1, 5));
	        jb_subEliminar.setBounds(200, 250, 100, 40); 
		}
		return jb_subEliminar;
	}
	
	private JComboBox<String> getJComboBoxMaterias() {
		if (jcb_matel == null) {
			String[] items = {};
			jcb_matel = new JComboBox<String>(items);
			jcb_matel.setBackground(Color.white);
	        jcb_matel.setBounds(50, 162, 425, 20); 
		}
		return jcb_matel;
	}
	
	private JLabel getJLabelIntro() {
		if (jlb_intro == null) {
			jlb_intro = new JLabel("Cargar Horarios");
			Font newTextFieldFont=new Font(jlb_intro.getFont().getName(),jlb_intro.getFont().getStyle(),18);   
		    jlb_intro.setFont(newTextFieldFont);  
		    jlb_intro.setBounds(50, 20, 400, 50);
		}

		return jlb_intro;
	}
	
	private JLabel getJLabelIntro2() {
		if (jlb_intro2 == null) {
			jlb_intro2 = new JLabel("Cargar Horarios");
			Font newTextFieldFont=new Font(jlb_intro2.getFont().getName(),jlb_intro2.getFont().getStyle(),18);   
		    jlb_intro2.setFont(newTextFieldFont);  
		    jlb_intro2.setBounds(50, 20, 400, 50);
		}

		return jlb_intro2;
	}
	
	private JLabel getJLabelIntro3() {
		if (jlb_intro3 == null) {
			jlb_intro3 = new JLabel("Cargar Horarios");
			Font newTextFieldFont=new Font(jlb_intro3.getFont().getName(),jlb_intro3.getFont().getStyle(),18);   
		    jlb_intro3.setFont(newTextFieldFont);  
		    jlb_intro3.setBounds(50, 20, 400, 50);
		}

		return jlb_intro3;
	}
	
	private JButton getJButtonEliminar() {
		if (jb_eliminar== null) {
			jb_eliminar = new JButton();
			jb_eliminar.setText("Eliminar Inscripcion");
	        jb_eliminar.setMargin(new Insets(1, 5, 1, 5));
	        jb_eliminar.setBounds(150, 370, 200, 40); 
		}
		return jb_eliminar;
	}
	
	private JLabel getJLabelDepto() {
		if (jlb_depto == null) {
			jlb_depto = new JLabel("Departamento:");
			Font newTextFieldFont=new Font(jlb_depto.getFont().getName(),jlb_depto.getFont().getStyle(),14);   
		    jlb_depto.setFont(newTextFieldFont);  
		    jlb_depto.setBounds(75, 150, 150, 50);
		}

		return jlb_depto;
	}
	
	private JButton getJButtonSiguienteMat() {
		if (jbt_sigMat== null) {
			jbt_sigMat = new JButton();
			jbt_sigMat.setText("Siguiente");
	        jbt_sigMat.setMargin(new Insets(1, 5, 1, 5));
	        jbt_sigMat.setBounds(200, 250, 100, 40); 
		}
		return jbt_sigMat;
	}
	
	protected JComboBox<String> getJComboBoxDepto(){
		
		if (jcb_depto == null) {
			
			String[] items = {};
			jcb_depto = new JComboBox<String>(items);
			jcb_depto.setBackground(Color.white);
	        jcb_depto.setBounds(200, 162, 300, 20); 
		}
		return jcb_depto;
	}
	
	private JLabel getJLabelMateria() {
		if (jlb_materia == null) {
			jlb_materia = new JLabel("Materia:");
			Font newTextFieldFont=new Font(jlb_materia.getFont().getName(),jlb_materia.getFont().getStyle(),14);   
		    jlb_materia.setFont(newTextFieldFont);  
		    jlb_materia.setBounds(75, 150, 150, 50);
		}

		return jlb_materia;
	}
	
	private JButton getJButtonSiguienteCurso() {
		if (jbt_sigCurso== null) {
			jbt_sigCurso = new JButton();
			jbt_sigCurso.setText("Siguiente");
	        jbt_sigCurso.setMargin(new Insets(1, 5, 1, 5));
	        jbt_sigCurso.setBounds(200, 250, 100, 40); 
		}
		return jbt_sigCurso;
	}
	
	private JComboBox<String> getJComboBoxMateria(){
		
		if (jcb_materia == null) {
			String[] items = {};
			jcb_materia = new JComboBox<String>(items);
			jcb_materia.setBackground(Color.white);
	        jcb_materia.setBounds(150, 162, 300, 20); 
		}
		return jcb_materia;
	}
	
	private JLabel getJLabelCurso() {
		if (jlb_curso == null) {
			jlb_curso = new JLabel("Curso:");
			Font newTextFieldFont=new Font(jlb_curso.getFont().getName(),jlb_curso.getFont().getStyle(),14);   
		    jlb_curso.setFont(newTextFieldFont);  
		    jlb_curso.setBounds(75, 150, 150, 50);
		}

		return jlb_curso;
	}
	
	private JButton getJButtonCargar() {
		if (jbt_cargar== null) {
			jbt_cargar = new JButton();
			jbt_cargar.setText("Cargar");
	        jbt_cargar.setMargin(new Insets(1, 5, 1, 5));
	        jbt_cargar.setBounds(200, 250, 100, 40); 
		}
		return jbt_cargar;
	}
	
	private JComboBox<String> getJComboBoxCurso(){
		
		if (jcb_curso == null) {
			String[] items = {};
			jcb_curso = new JComboBox<String>(items);
			jcb_curso.setBackground(Color.white);
	        jcb_curso.setBounds(150, 162, 300, 20); 
		}
		return jcb_curso;
	}
	
	private void SigMateriaActionListener(ActionListener listener) {
		jbt_sigMat.addActionListener(listener);	
	}
	
	private void SigCursoActionListener(ActionListener listener) {
		jbt_sigCurso.addActionListener(listener);	
	}
	
	private void CargarActionListener(ActionListener listener) {
		jbt_cargar.addActionListener(listener);	
	}
	
	private void EliminarActionListener(ActionListener listener) {
		jb_eliminar.addActionListener(listener);	
	}
	
	private void SubEliminarActionListener(ActionListener listener) {
		jb_subEliminar.addActionListener(listener);	
	}
	
	public JComponent getMainComponent() {
	      return mainPanel;
	}


}
